package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void deductBalanceById(Long id, Integer amount) {
        //1. 判断用户是否存在
        User user = this.getById(id);
        if (user == null){
            throw new RuntimeException("用户不存在");
        }
        if(user.getStatus() == UserStatus.FROZEN){
            throw new RuntimeException("用户被冻结");
        }
        //2. 判断账户余额是否充足
        if (user.getBalance() < amount){
            throw new RuntimeException("余额不足");
        }

//        user.setBalance(user.getBalance() - amount);
//        updateById(user);

//        userMapper.deductBalanceById(amount, id);
        int remainBalance = user.getBalance() - amount;
        this.lambdaUpdate()
                .eq(User::getId, id)
                .eq(User::getBalance, user.getBalance())
                .set(User::getBalance, remainBalance)
                .set(remainBalance == 0, User::getStatus, 2)
                .update();
    }

    @Override
    public UserVO queryUserAndAddressById(Long id) {
        UserVO userVO = BeanUtil.copyProperties(this.getById(id), UserVO.class);
        List<Address> addresses = Db.lambdaQuery(Address.class)
                .eq(Address::getUserId, id)
                .list();
        userVO.setAddresses(BeanUtil.copyToList(addresses, AddressVO.class));
        return userVO;
    }

    @Override
    public List<UserVO> queryUserAndAddressByIds(List<Long> ids) {
        List<User> users = this.listByIds(ids);
        List<UserVO> userVOS = BeanUtil.copyToList(users, UserVO.class);
        /*for (UserVO userVO : userVOS) {
            List<Address> addresses = Db.lambdaQuery(Address.class)
                    .eq(Address::getUserId, userVO.getId())
                    .list();
            userVO.setAddresses(BeanUtil.copyToList(addresses, AddressVO.class));
        }*/
        List<Address> addresses = Db.lambdaQuery(Address.class)
                .in(Address::getUserId, ids)
                .list();
        Map<Long, List<AddressVO>> userAddressMap = BeanUtil.copyToList(addresses, AddressVO.class)
                .stream()
                .collect(Collectors.groupingBy(AddressVO::getUserId));
        for (UserVO userVO : userVOS) {
            List<AddressVO> addressVOS = userAddressMap.get(userVO.getId());
            userVO.setAddresses(addressVOS);
        }
        return userVOS;
    }
}
